package com.tdx.blog_nowcoder.controller;


import com.github.benmanes.caffeine.cache.Cache;

import com.tdx.blog_nowcoder.entity.User;
import com.tdx.blog_nowcoder.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.Collection;

/**
 * @program: double-cache
 * @author: Hydra
 * @create: 2022-03-11 13:32
 **/
@RestController
@RequestMapping("/testuser")
@AllArgsConstructor
public class TestCacheController {

    @Autowired
    private UserService userService;

//    @GetMapping("/test")
//    public String test(){
//        return "hello world";
//    }

    @GetMapping("get/{id}")
    public User get(@PathVariable("id") int id){
        System.out.println(id);
        return userService.selectUserById(id);
    }

    @GetMapping("del/{id}")
    public int del(@PathVariable("id") int id){
        return userService.deleteUserById(id);
    }

//    @PostMapping("update")
//    public void updateOrder(@RequestBody Order order){
//        orderService.updateOrder(order);
//    }
//
//    @DeleteMapping("del")
//    public void del(@RequestParam("id") Long id){
//        orderService.deleteOrder(id);
//    }
//
//    @GetMapping("get2/{id}")
//    public Order get2(@PathVariable("id") Long id){
//        return orderService.getOrderById2(id);
//    }
//
//    @GetMapping("clear")
//    public void clearCache() throws Exception {
//        MyDoubleCacheManager cacheManager = SpringContextUtil.getBean(MyDoubleCacheManager.class);
//        MyDoubleCache doubleCache = (MyDoubleCache) cacheManager.getCache("order");
//
//        Class<MyDoubleCache> doubleCacheClass = MyDoubleCache.class;
//        Field caffeineCacheField = doubleCacheClass.getDeclaredField("caffeineCache");
//        caffeineCacheField.setAccessible(true);
//        Cache caffeineCache = (Cache) caffeineCacheField.get(doubleCache);
//        System.out.println(caffeineCache.asMap());
//
//        doubleCache.clear();
//        System.out.println("clear...");
//
//        System.out.println(caffeineCache.asMap());
//    }
//
//
//    @GetMapping("name")
//    public void cacheName(){
//        MyDoubleCacheManager cacheManager = SpringContextUtil.getBean(MyDoubleCacheManager.class);
//        Collection<String> cacheNames = cacheManager.getCacheNames();
//        System.out.println(cacheNames);
//    }



}
